//
//  VehicleInfoQueryViewController.m
//  CarLife
//
//  Created by yu on 2017/10/30.
//  Copyright © 2017年 jpas. All rights reserved.
//

#import "VehicleInfoQueryViewController.h"
#import "SelectCityViewController.h"
#import "VehicleInfoCell.h"
#import "ProvinceAbbrView.h"
#import "CityModel.h"
#import "QueryResultViewController.h"

/*
 烟台车辆：
 车牌号：鲁Y815S1
 发动机号：141931
 车架号：141509
 */
@interface VehicleInfoQueryViewController ()<UITableViewDelegate,UITableViewDataSource,VehicleInfoHandleDelegate,ProvinceAbbrDelegate,SelectCityDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (nonatomic, strong) KLCPopup *popup;

@property (nonatomic, strong) KLCPopup *popup_s;

@property (nonatomic, strong) ProvinceAbbrView *proAbbrView;

@property (nonatomic, strong) VehicleInfoCell *cell;

@property (nonatomic, strong) UIButton *abbrBtn;

@property (weak, nonatomic) UITextField *platenoTF;
@property (weak, nonatomic) UILabel *cityLabel;
@property (weak, nonatomic) UITextField *engineTF;
@property (weak, nonatomic) UITextField *vinTF;

@property (nonatomic, strong) CityModel *cityModel;

// 参数
@property (nonatomic, copy) NSString *vehicleType;
@property (nonatomic, copy) NSString *plateno;
@property (nonatomic, copy) NSString *city;
@property (nonatomic, copy) NSString *classno;
@property (nonatomic, copy) NSString *engineno;

@end

@implementation VehicleInfoQueryViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"添加车辆";
    [self setLeftImageNamed:@"back_gray" action:@selector(returnAction)];
    [self setRightItemTitle:@"开始查询" titleColor:kBaseColor action:@selector(queryAction)];
    [self setupPopupView];
    [self setupPopupsView];
}

- (void)queryAction {

    [self.platenoTF resignFirstResponder];
    [self.vinTF resignFirstResponder];
    [self.engineTF resignFirstResponder];
    
    // 判断车牌号
    if (self.platenoTF.text.length != 6) {
        [MBProgressHUD showToast:@"车牌号不完整"];
        return;
    } else {
//        PPLog(@"%@",self.abbrBtn.titleLabel.text);
        self.plateno = NSStringFormat(@"%@%@",self.abbrBtn.titleLabel.text,self.platenoTF.text);
    }
    
    // 判断城市
    if (self.cityLabel.text.length == 8) {
        [MBProgressHUD showToast:@"请选择城市"];
        return;
    } else {
        self.city = self.cityModel.cityCode;
    }
    
    // 判断车架号
    if ([self.cityModel.class1 isEqualToString:@"0"]) {
        self.classno = @"";
    } else {//需要车架号
        
        ///<1>.需要完整的车架号
        if ([self.cityModel.classno isEqualToString:@"0"]) {
            if (self.vinTF.text.length != 17) {
                [MBProgressHUD showToast:NSStringFormat(@"须输入完整车架号")];
                return;
            } else {
                self.classno = self.vinTF.text;
            }
        } else {///<2>.车架号后N位
            if (self.vinTF.text.length != [self.cityModel.classno intValue]) {
                [MBProgressHUD showToast:NSStringFormat(@"须输入车架号后%@位",self.cityModel.classno)];
                return;
            } else {
                self.classno = self.vinTF.text;
            }
        }
    }
    
    // 判断发动机号
    if ([self.cityModel.engine isEqualToString:@"0"]) {//0不需要发动机号
        self.engineno = @"";
    } else {//需要发动机号
        
        ///<1>.需要完整发动机号
        if ([self.cityModel.engineno isEqualToString:@"0"]) {
            if (!self.engineTF.text.length) {
                [MBProgressHUD showToast:NSStringFormat(@"须输入完整发动机号")];
                return;
            } else {
                self.engineno = self.engineTF.text;
            }
        } else {///<2>.需要发动机号后N位
            if (self.engineTF.text.length != [self.cityModel.engineno intValue]) {
                [MBProgressHUD showToast:NSStringFormat(@"须输入发动机号后%@位",self.cityModel.engineno)];
                return;
            } else {
                self.engineno = self.engineTF.text;
            }
        }
    }
    
    [MBProgressHUD showHUDAddedTo:KEY_WINDOW animated:YES];
    [self.requestTool queryVehicleInfoByCity:self.city hphm:self.plateno hpzl:@"02" engineno:self.engineno classno:self.classno success:^(NSMutableArray *resultList,NSString *tip) {
        [MBProgressHUD hideHUDForView:KEY_WINDOW];
//        PPLog(@"违章个数 = %ld",resultList.count);
        if (tip.length) {
            [[[UIAlertView alloc] initWithTitle:nil message:tip delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil] show];
            return;
        }
        
        if (resultList) {
            QueryResultViewController *result = [[QueryResultViewController alloc] init];
            result.plateno = self.plateno;
            result.resultList = resultList;
            [self.navigationController pushViewController:result animated:YES];
        }
    } failure:^(NSString *error) {
        [MBProgressHUD hideHUDForView:KEY_WINDOW];
        [[[UIAlertView alloc] initWithTitle:nil message:@"未知错误" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil] show];
    }];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self setWhiteNavigationBar];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self setDefaultNavigationBar];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 5;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
//    PPLog(@"车架号：%@-发动机号：%@",self.cityModel.class1,self.cityModel.engine);
    if (indexPath.row == 4) {
        if ([self.cityModel.class1 isEqualToString:@"0"]) {
            return 0;
        }
    } else if (indexPath.row == 3) {
        if ([self.cityModel.engine isEqualToString:@"0"]) {
            return 0;
        }
    }
    return 65;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    VehicleInfoCell *cell = [VehicleInfoCell initTableViewCellWith:tableView indexPath:indexPath];
    cell.delegate = self;
    
    if (indexPath.row == 1) {
        self.abbrBtn = cell.abbrBtn;
        self.platenoTF = cell.platenoTF;
    } else if (indexPath.row == 2) {
        self.cityLabel = cell.cityLabel;
    } else if (indexPath.row == 3) {
        self.engineTF = cell.engineTF;
    } else if (indexPath.row == 4) {
        self.vinTF = cell.vinTF;
    }
    return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UILabel *noteLabel = [[UILabel alloc] init];
    noteLabel.text = @"请对照《机动车行驶证》认真填写如下信息表";
    noteLabel.textColor = [UIColor whiteColor];
    noteLabel.textAlignment = NSTextAlignmentCenter;
    noteLabel.font = [UIFont systemFontOfSize:13];
    noteLabel.backgroundColor = kBaseColor;
    noteLabel.sd_layout.spaceToSuperView(UIEdgeInsetsZero);
    return noteLabel;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 25;
}

- (void)setupPopupView
{
    if (self.popup == nil) {
        UIImageView *licenseView = [[UIImageView alloc] init];
        licenseView.image = IMAGE_NAMED(@"drive_license_example");
        licenseView.size_sd = CGSizeMake(320, 220);
    
        self.popup = [KLCPopup popupWithContentView:licenseView
                                           showType:KLCPopupShowTypeShrinkIn
                                        dismissType:KLCPopupDismissTypeFadeOut
                                           maskType:KLCPopupMaskTypeDimmed
                           dismissOnBackgroundTouch:YES
                              dismissOnContentTouch:YES];
    }
}

- (void)setupPopupsView {
    if (self.popup_s == nil) {
        ProvinceAbbrView *proAbbrView = [[ProvinceAbbrView alloc] init];
        proAbbrView.delegate = self;
        proAbbrView.size_sd = CGSizeMake(kScreenWidth, 170);
        
        self.popup_s = [KLCPopup popupWithContentView:proAbbrView
                                           showType:KLCPopupShowTypeSlideInFromBottom
                                        dismissType:KLCPopupDismissTypeSlideOutToBottom
                                           maskType:KLCPopupMaskTypeNone
                           dismissOnBackgroundTouch:YES
                              dismissOnContentTouch:NO];
    }
}

#pragma mark - VehicleInfoHandleDelegate

- (void)showVehicleTypeAction:(NSString *)type {
//    PPLog(@"车类型:%@",type);
    self.vehicleType = type;
}

- (void)showProvinceAbbrAction {
    [self.popup_s showWithLayout:KLCPopupLayoutMake(KLCPopupHorizontalLayoutCenter, KLCPopupVerticalLayoutBottom)];
}

- (void)showLicenseExampleAction {
    [self.popup showWithLayout:KLCPopupLayoutMake(KLCPopupHorizontalLayoutCenter, KLCPopupVerticalLayoutCenter)];
}

- (void)showCityPageAction {
    SelectCityViewController *city = [[SelectCityViewController alloc] init];
    city.delegate = self;
    [self.navigationController pushViewController:city animated:YES];
}

#pragma mark -SelectCityDelegate
- (void)chooseCityModel:(CityModel *)cityModel {
    self.cityModel = cityModel;
//    PPLog(@"delegate city name = %@",cityModel.cityName);
    self.cityLabel.text = cityModel.cityName;
    if (@available(iOS 13.0, *)) {
        self.cityLabel.textColor = UIColor.labelColor;
    } else {
        self.cityLabel.textColor = kRgb(30, 30, 30);
    }
    self.cityLabel.font = Font_NAME_SIZE(@"PingFangSC-Medium", 16);
    
    // 需要几位发动机号0:全部 1-9 :需要发动机号后N位
    if ([cityModel.engineno isEqualToString:@"0"]) {
        [self.engineTF setPlaceholder:NSStringFormat(@"输入完整发动机号")];
    } else {
       [self.engineTF setPlaceholder:NSStringFormat(@"输入发动机号后%@位",cityModel.engineno)];
    }
    
    if ([cityModel.classno isEqualToString:@"0"]) {
        [self.vinTF setPlaceholder:NSStringFormat(@"输入完整车架号")];
    } else {
        [self.vinTF setPlaceholder:NSStringFormat(@"输入车架号后%@位",cityModel.classno)];
    }
    
    [self.tableView reloadData];
}

#pragma mark -ProvinceAbbrDelegate

- (void)chooseProvinceAbbr:(NSString *)abbr {
    [self.abbrBtn setTitle:abbr forState:UIControlStateNormal];
    [self.popup_s dismiss:YES];
}

- (void)finishAction {
    [self.popup_s dismiss:YES];
}

- (void)returnAction {
    [self.platenoTF resignFirstResponder];
    [self.vinTF resignFirstResponder];
    [self.engineTF resignFirstResponder];
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
